//
//  TimerCenter.swift
//
//  Created by ink on 2024/6/27.
//  Copyright © 2024 ZM. All rights reserved.
//

import UIKit

extension NSNotification.Name {
    static let TimerCenterActionSeconds = NSNotification.Name("TimerCenterActionSeconds")
}

class TimerCenter: NSObject {
    
    private var difference: Int64 = 0
    
    ///定时器
    private var timer: Timer?
    
    ///
    static let shared: TimerCenter = {
        let instance = TimerCenter()
        instance.timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { [weak instance] timer in
            instance?.timerSelector()
        })
        // setup code
        return instance
    }()
    
    func currentTimeStamp() ->Int64 {
        return Int64(Date().timeIntervalSince1970) + difference
    }
    
    /// 同步服务器时间
    func synchronizingServerTime() {
       
    }
    
    class func publishCountDown(publishTime: String?) -> (countDown:Int64, publishTimeStr:String)?{
        /// 即将发售
        guard let publishTime = publishTime else {
            return nil
        }
                
        /// 发售倒计时
        let countDown = Date.getTimestmp(timeString: publishTime) - TimerCenter.shared.currentTimeStamp()
        
        return (countDown, publishTime)
    }
    
    ///定时器调用
    func timerSelector() {
        let currenttime = Int64(Date().timeIntervalSince1970) + difference
        NotificationCenter.default.post(name: .TimerCenterActionSeconds, object: "\(currenttime)")
    }
}
